//
//  ViewController.m
//  lehuotest
//
//  Created by ice on 2016/12/20.
//  Copyright © 2016年 QF. All rights reserved.
//

#import "ViewController.h"
#import "ThirdViewController.h"
#import "LHActionSheet.h"
#import "LHAlertView.h"
#import <Masonry.h>
#import <MJRefresh.h>
#import "LHDetailHeaderView.h"
#import "LHhorizontalCell.h"
//#import "UIViewController+KNSemiModal.h"
#import "LHSkuView.h"
#import <UINavigationBar+Awesome.h>
#import "LHImageTextDetailView.h"
#import "HomeGetAttrModel.h"
#import "UDButton.h"
#import "GuideView.h"
#import "StartADView.h"
//#import "SVProgressHUD+Add.h"
#import "MBProgressHUD+Add.h"
#import "LHProgressHUD.h"
#import <JZNavigationExtension.h>
#import "SecondViewController.h"
#import "UIViewController+Sunken.h"
#import "LHGoodsDetailView.h"
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

#define kEndH 40

#define kImageTextEndH 50

#define kNavBarHeight   0
#define kHeaderViewHeight 400
#define NAVBAR_CHANGE_POINT -kHeaderViewHeight
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate,LHImageTextDetailViewDelegate>
{
    CGFloat minY;
    CGFloat maxY;
    // 是否显示底部视图，
    BOOL _isShowBottom;
}

@property (nonatomic, strong) UITableView *table;

@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) UIView *headerView;

@property (nonatomic, strong) UIView *navBarView;

@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UILabel *middleLab;
@property (nonatomic, strong) UIWebView *webView;
@property (nonatomic, strong) UILabel *bottomLab;
@property (nonatomic, strong) LHImageTextDetailView *imageTextView;

@property (nonatomic, assign) BOOL hasShowImageText;

@property (nonatomic, strong) LHGoodsDetailView *goodsDetailView;

@end

@implementation ViewController

#pragma mark - 懒加载
-(UIView *) navBarView
{
    if (!_navBarView){
        _navBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
        _navBarView.backgroundColor = [UIColor whiteColor];
        
        UDButton *btn = [UDButton buttonWithType:UIButtonTypeCustom];
//        btn.backgroundColor = [UIColor greenColor];
        btn.frame = CGRectMake(SCREEN_WIDTH-80, 20, 80, 44);
//        [btn setBackgroundImage:[UIImage imageNamed:@"icon_child_01"] forState:UIControlStateNormal];
        [btn setTitle:@"popView" forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"icon_child_01"] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(next) forControlEvents:UIControlEventTouchUpInside];
        [_navBarView addSubview:btn];

        
    }
    return _navBarView;
}
-(NSMutableArray *) dataSource
{
    if (!_dataSource){
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

-(void) next
{
    HomeGetAttrModel *model = [[HomeGetAttrModel alloc] init];
    model.attr_id = 1;
    model.attr_name = @"樵夫";
    HomeAttValuesModel *item1 = [[HomeAttValuesModel alloc] init];
    item1.label = @"asdhfjkasdfjjjjj";
    item1.price = @"11.11";
    item1.ID = 2;
    
    HomeAttValuesModel *item2 = [[HomeAttValuesModel alloc] init];
    item2.label = @"222222";
    item2.price = @"222.222";
    item2.ID = 2;
    model.values = @[item1,item2,item1,item1,item1];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.jz_wantsNavigationBarVisible = NO;
    
    //引导页
    [GuideView show];
    
    //启动页广告
    [StartADView showWithTapBlock:^(NSString *urlString) {
        NSLog(@"跳转链接 = %@",urlString);
    }];

    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    UITableView *table = [[UITableView alloc] init];
    table.frame = CGRectMake(0, -kNavBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT);
    table.delegate = self;
    table.dataSource = self;
    self.table = table;

    
    LHGoodsDetailView *vv = [[LHGoodsDetailView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) topView:self.table bootomView:nil];

    self.goodsDetailView = vv;
    [self.view addSubview:vv];
    
    [self loadData];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.backgroundColor = [UIColor blackColor];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btn.frame = CGRectMake(0, 0, 100, 50);
    [btn setTitle:@"樵夫测试" forState:UIControlStateNormal];
    [self.view addSubview:btn];
    [btn addTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn1.backgroundColor = [UIColor blackColor];
    [btn1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btn1.frame = CGRectMake(SCREEN_WIDTH-100, 0, 100, 50);
    [btn1 setTitle:@"雪永测试" forState:UIControlStateNormal];
    [self.view addSubview:btn1];
    [btn1 addTarget:self action:@selector(next1) forControlEvents:UIControlEventTouchUpInside];
    
    
}
-(void) next1
{
    ThirdViewController *thirdVC = [[ThirdViewController alloc] init];
    [self presentViewController:thirdVC animated:YES completion:nil];

}
-(void) btnAction
{
    [LHProgressHUD showHudToView:nil];
}
-(LHImageTextDetailView *)imageTextView
{
    if (!_imageTextView){
        _imageTextView = [LHImageTextDetailView loadFromNib];
        _imageTextView.frame = CGRectMake(0, SCREEN_HEIGHT+64, SCREEN_WIDTH, SCREEN_HEIGHT-64);
        _imageTextView.webView.scrollView.delegate = self;
        _imageTextView.delegate = self;
    }
    return _imageTextView;
}

- (UIView *)contentView
{
    if (!_contentView)
    {
        _contentView = [[UIView alloc] init];
        _contentView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT * 2);
        _contentView.backgroundColor = [UIColor whiteColor];
        
    }
    return _contentView;
}
- (UILabel *)middleLab
{
    if (!_middleLab)
    {
        _middleLab = [[UILabel alloc] initWithFrame:CGRectMake(0, self.table.contentSize.height+kHeaderViewHeight, SCREEN_WIDTH, kEndH)];
        _middleLab.font = [UIFont systemFontOfSize:13.0f];
        _middleLab.textAlignment = NSTextAlignmentCenter;
        _middleLab.backgroundColor = [UIColor whiteColor];
        _middleLab.textColor = [UIColor blackColor];
    }
    return _middleLab;
}


-(void) loadData{
    
    if (self.dataSource.count > 20){
        return;
    }
    
    for (int i = 0 ; i<10; i++) {
        [self.dataSource addObject:@""];
    }
    [self.table reloadData];
}

-(void) refreshData{
    [self.dataSource removeAllObjects];
    [self loadData];
}
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 140;
}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LHhorizontalCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LHhorizontalCell class])];
    if (cell == nil){
        cell = [LHhorizontalCell loadFromNib];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.backgroundColor = [UIColor yellowColor];
    return cell;
}


-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    SecondViewController *vc =[[SecondViewController alloc] init];
    vc.jz_wantsNavigationBarVisible = YES;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UIScrollerViewDelegate /*这里针对中间部分**/

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
     [self.goodsDetailView LHGoodsDetailViewScrollViewDidEndDragging:scrollView willDecelerate:decelerate];
}

#pragma mark - LHImageTextDetailViewDelegate

-(void) LHImageTextDetailView:(LHImageTextDetailView *)imageTextView loadRequest:(NSString *)url isSuccess:(BOOL)isSuccess
{
    self.hasShowImageText = isSuccess;
}
@end
